package com.china08.yxyapi.rabbitmq.listener.swsstatistics;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.config.SwsStatisticsConfig;
import com.china08.yxyapi.service.sws.teaching.impl.EsbagTeachingStatisticsServiceImpl;
import com.china08.yxyapi.service.sws.teaching.impl.ExamStatisticsServiceImpl;
import com.china08.yxyapi.service.sws.teaching.impl.HomeworkStatisticsServiceImpl;
import com.china08.yxyapi.service.sws.teaching.impl.LecturePackStatisticsServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 教学统计监听
 *
 */
@Component
@EnableRabbit
public class TeachingStatisticsListener {

    public static final Logger LOGGER = LoggerFactory.getLogger(TeachingStatisticsListener.class);

    @Autowired
    private ExamStatisticsServiceImpl examStatisticsServiceImpl;

    @Autowired
    private HomeworkStatisticsServiceImpl homeworkStatisticsServiceImpl;

    @Autowired
    private LecturePackStatisticsServiceImpl lecturePackStatisticsServiceImpl;

    @Autowired
    private EsbagTeachingStatisticsServiceImpl esbagTeachingStatisticsServiceImpl;

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    /**
     * 业务统计——教学统计
     * 
     * @param date
     * @throws ParseException
     */
    @RabbitListener(queues = SwsStatisticsConfig.YW_SWS_TEACHING_STATISTICS)
    private void teachingStatisticsListener(Integer date) throws ParseException {

        // exam统计
        examStatisticsServiceImpl.getExamImportScoreForClass(date);
        examStatisticsServiceImpl.getExamSendScoreForClass(date);
        examStatisticsServiceImpl.getExamImportScoreForSchool(date);
        examStatisticsServiceImpl.getExamSendScoreForSchool(date);
        examStatisticsServiceImpl.getExamImportScoreForSubjectAndStage(date);
        examStatisticsServiceImpl.getExamSendScoreForSubjectAndStage(date);
        examStatisticsServiceImpl.getExamImportScoreForUser(date);
        examStatisticsServiceImpl.getExamSendScoreForUser(date);

        // homgwork
        homeworkStatisticsServiceImpl.getHomeworkForClass(date);
        homeworkStatisticsServiceImpl.getHomeworkForSchool(date);
        homeworkStatisticsServiceImpl.getHomeworkForSubjectAndStage(date);
        homeworkStatisticsServiceImpl.getHomeworkForUser(date);
        homeworkStatisticsServiceImpl.getHomeworkForLectureSource(date);
        homeworkStatisticsServiceImpl.getHomeworkForHomeworkSource(date);
        homeworkStatisticsServiceImpl.getHomeworkForTeacherAndStage(date);

        // pack
        lecturePackStatisticsServiceImpl.getPackForClass(date);
        lecturePackStatisticsServiceImpl.getPackForSchool(date);
        lecturePackStatisticsServiceImpl.getPackForSubjectAndStage(date);
        lecturePackStatisticsServiceImpl.getPackForUser(date);
        lecturePackStatisticsServiceImpl.getLectureUseType(date);
        lecturePackStatisticsServiceImpl.getPerPackForUser(date);// new

        // Esbag
        esbagTeachingStatisticsServiceImpl.getEsbagForClass(date);
        esbagTeachingStatisticsServiceImpl.getEsbagForSchool(date);
        esbagTeachingStatisticsServiceImpl.getEsbagForUser(date);

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(date));

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateTime", DateUtils.formatDate4(dayDate));
        HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(map, headers);
        String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/api_yx/comm/school/sws/teaching/mq", formEntity,
                String.class);
        if (httpResult.contains("success")) {
            System.out.println("班级圈触发mq执行ok,日期：" + DateUtils.formatDate4(dayDate));
        }
    }

}
